package ar.com.larctest.forms;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.adapters.SpinnerWorkAdapter;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.Work;
import ar.com.larctest.providers.WorkProvider;
import ar.com.larctest.providers.temp.MeasurementTempProvider;

public class PopUpFinalMeasurementActivity extends Activity {
    private static final String TAG = "PopUpFinalMeasurementActivity";

    List<Work> lstWork = new ArrayList<Work>();
    Integer actualHorse;

    Spinner spiWork;
    EditText etMeters;
    EditText etTime;

    Work selectedWork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_up_final_measurement);
        Bundle bundle = getIntent().getExtras();

        try
        {
            actualHorse =  bundle.getInt("actualHorse");
            spiWork = (Spinner) findViewById(R.id.work);
            etMeters = (EditText) findViewById(R.id.meters);
            etTime = (EditText) findViewById(R.id.time);

            WorkProvider workProvider = new WorkProvider(this);
            lstWork = workProvider.getWorks();
            SpinnerWorkAdapter workAdapter =  new SpinnerWorkAdapter(this, android.R.layout.simple_spinner_item, lstWork);
            spiWork.setAdapter(workAdapter);
            spiWork.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
                {
                    selectedWork = lstWork.get(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    selectedWork = null;
                }
            });

        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    public void onClickExit(View view)
    {
        setResult(RESULT_CANCELED);
        this.finish();
    }

    public void onClickSave(View view)
    {
        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(this);

        if(selectedWork == null) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Measurement_Popup_msgInputWork));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else if(etMeters.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Measurement_Required_Meters));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else
        {
            sqlHelper.dbOpen();

            try
            {
                sqlHelper.beginTransaction();

                MeasurementTempProvider measurementTempProvider = new MeasurementTempProvider(this);

                Integer idWork = 0;
                Double dblMeters = 0D;
                Double dblTime = 0D;
                if(selectedWork != null)
                    idWork = selectedWork.getIdWork();

                if(etMeters.getText() != null)
                    dblMeters = Double.parseDouble(etMeters.getText().toString());

                if(etTime.getText() != null)
                    dblTime = Double.parseDouble(etTime.getText().toString());

                measurementTempProvider.updateFinalMeasurementItem(actualHorse, idWork, dblMeters, dblTime);

                sqlHelper.commitTransaction();

                setResult(RESULT_OK);
                this.finish();
            }
            catch (Exception e) {
                sqlHelper.rollbackTransaction();
                Log.e(TAG, e.toString());
            }
            finally {
                sqlHelper.dbClose();
            }
        }
    }
}
